cmake_minimum_required(VERSION 3.0.2)
project(ros_midware)

set(DEFAULT_BUILD_TYPE "Release")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-w")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -g -ggdb ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g ${CMAKE_CXX_FLAGS}")

find_package(OpenCV 3 QUIET)
if (NOT OpenCV_FOUND)
    find_package(OpenCV 4 REQUIRED)
endif ()

# yaml-cpp
find_package(yaml-cpp REQUIRED)

# -------------------- ROS
find_package(catkin REQUIRED COMPONENTS
    roscpp
    rospy
    std_msgs
    sensor_msgs
    geometry_msgs
    cv_bridge
    actionlib
    move_base_msgs
    yhs_msgs
)

add_definitions(-DROS_AVAILABLE=1)
catkin_package(
    INCLUDE_DIRS src/
    LIBRARIES ros_midware
    CATKIN_DEPENDS roscpp rospy std_msgs sensor_msgs geometry_msgs cv_bridge actionlib move_base_msgs yhs_msgs
)

include_directories(
    src
    src/basic
    src/protocol
    ${YAML_CPP_INCLUDE_DIR}
    ${OpenCV_INCLUDE_DIRS}
    ${catkin_INCLUDE_DIRS}
)

list(APPEND thirdparty_libraries
    glog 
    gflags 
    pthread
    jsoncpp
    protobuf
    ${OpenCV_LIBRARIES}
    ${YAML_CPP_LIBRARIES}
)

# ------------ robot_lib
list(APPEND LIBRARY_SOURCES
    src/basic/tools.cc
    src/basic/LoopFunc.cc
    src/basic/UdpServer.cc
)

file(GLOB_RECURSE LIBRARY_HEADERS "src/*.h")
add_library(robot_lib SHARED ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
target_link_libraries(robot_lib ${thirdparty_libraries})
target_include_directories(robot_lib PUBLIC src/)

add_executable(run_robot src/run_robot.cc src/robot/robot.cc)
target_link_libraries(run_robot robot_lib ${thirdparty_libraries})

add_executable(run_ros1 src/run_ros1.cc src/ros/ros1node.cc)
target_link_libraries(run_ros1 robot_lib ${thirdparty_libraries} ${catkin_LIBRARIES})

add_executable(run_udp_client src/run_udp_client.cc)
target_link_libraries(run_udp_client robot_lib ${thirdparty_libraries})

add_executable(simple_goal src/simple_goal.cc)
target_link_libraries(simple_goal robot_lib ${catkin_LIBRARIES})

install(TARGETS robot_lib run_robot run_ros1 simple_goal run_udp_client 
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
install(DIRECTORY src/
    DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
    FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)
install(DIRECTORY launch/
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
)